//
//  EAppDetailRankingView.m
//  AppRanking
//
//  Created by Gang.Wang on 13-1-14.
//  Copyright (c) 2013年 Gang.Wang. All rights reserved.
//

#import "EAppDetailRankingView.h"
#import "mcf.h"
#import "ERequestAppRanking.h"
#import "EAppRankingTableViewCell.h"

@interface EAppDetailRankingView()<UITableViewDataSource, UITableViewDelegate, YDownloadRequestDelegate>
{
    
}

@property (nonatomic, retain) UITableView * tableView;
@property (nonatomic, retain) UIButton * closeButton;

@property (nonatomic, retain) YDownloaderHelper * downloadHelper;

- (void) privateInit;

- (void) refreshAppRanking;

@end

@implementation EAppDetailRankingView

- (id) init
{
    self = [super init];
    if (self)
    {
        [self privateInit];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self privateInit];
    }
    return self;
}


- (void) setAppInfo:(EAppInfo *)appInfo
{
    if (!appInfo)
    {
        return;
    }
    
    YRelease(_appInfo);
    _appInfo = [appInfo retain];

    [self refreshAppRanking];
}


- (void) reAdjustLayout
{
    [self.closeButton setFrame:CGRectMake(10, 10, 40, 20)];
    [self.tableView setFrame:CGRectMake(0, 40, self.width, self.height - 45)];
}


#pragma mark - Private Method
- (void) privateInit
{
    if (self.bInited)
    {
        return;
    }
    
    YDownloaderHelper * temp_downloadHelper = [[YDownloaderHelper alloc] init];
    [temp_downloadHelper setMaxConcurrentTaskCount:5];
    
    self.downloadHelper = temp_downloadHelper;
    YRelease(temp_downloadHelper);
    
    self.closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    [self.closeButton setTitle:@"返回" forState:UIControlStateNormal];
    [self addSubview:self.closeButton];
    
    BUTTON_FUNCTION(self.closeButton, closeButtonFun)
    
    [self reAdjustLayout];
    self.bInited = YES;
}

- (void) closeButtonFun
{
    DELEGATE_CALLBACK(closeDetailRankingView:, nil);
}

- (void) refreshAppRanking
{
    if (!self.appInfo)
    {
        return;
    }
    
    
    [self.tableView setDelegate:nil];
    [self.tableView setDataSource:nil];
    
    [self.tableView removeFromSuperview];
    YRelease(self.tableView);
    
    UITableView * temp_tableView = [[UITableView alloc] init];
    
    [temp_tableView setDelegate:self];
    [temp_tableView setDataSource:self];
    
    self.tableView = temp_tableView;
    YRelease(temp_tableView);
    
    [self addSubview:self.tableView];
    
    
    
    [self reAdjustLayout];
}

- (ERequestAppRanking *) requestForSectionIndex:(NSInteger) section
{
    ERequestAppRanking * request = nil;
    
    if (!self.appInfo)
    {
        return request;
    }
    
    // ERequestAppRanking * request = [[ERequestAppRanking alloc] initWith:appInfo country:nil day:0 isiPhoneOs:appInfo.currentRequestPlatfromIsiPhone];
    switch (section)
    {
        case 0:
            request = [[[ERequestAppRanking alloc] initWith:self.appInfo country:nil day:0 isiPhoneOs:self.appInfo.currentRequestPlatfromIsiPhone] autorelease];
            break;
            
        case 1:
            request = [[[ERequestAppRanking alloc] initWith:self.appInfo country:nil day:1 isiPhoneOs:self.appInfo.currentRequestPlatfromIsiPhone] autorelease];
            break;
            
        case 2:
            request = [[[ERequestAppRanking alloc] initWith:self.appInfo country:nil day:2 isiPhoneOs:self.appInfo.currentRequestPlatfromIsiPhone] autorelease];
            break;
            
        case 3:
            request = [[[ERequestAppRanking alloc] initWith:self.appInfo country:nil month:2 isiPhoneOs:self.appInfo.currentRequestPlatfromIsiPhone] autorelease];
            break;
            
        case 4:
            request = [[[ERequestAppRanking alloc] initWith:self.appInfo country:nil month:3 isiPhoneOs:self.appInfo.currentRequestPlatfromIsiPhone] autorelease];
            break;
            
        case 5:
            
            break;
            
        default:
            break;
    }
    
    return request;
}

- (void) refreshRankingDataToCell:(ERequestAppRanking *) request
{
    if (!request)
    {
        return;
    }
    
    NSData * rankData = request.recvData.data;
    if (!rankData)
    {
        return;
    }
    
    ERanking * appRanking = [[[ERanking alloc] initWithData:rankData] autorelease];
    if (!appRanking)
    {
        return;
    }

    
    int section = [request.name integerValue];
    EAppRankingTableViewCell * cell = (EAppRankingTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
    
    if (!cell)
    {
        return;
    }
    
    [cell setAppRanking:appRanking];
    
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    /**
     今天
     昨天
     前天
     近30天
     近3个月
     */
    return 4;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSArray * titles = @[@"今天", @"昨天", @"前天", @"近30天", @"近3个月"];
    
    return [titles objectAtIndex:section];
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"RankingCell";
    
    DebugLog(@"[%d, %d]", indexPath.section, indexPath.row);
    EAppRankingTableViewCell *cell = (EAppRankingTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[EAppRankingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    [cell setIndexPath:indexPath];
        
    ERequestAppRanking * request = [self requestForSectionIndex:indexPath.section];
    request.name = [NSString stringWithFormat:@"%d", indexPath.section];
    
    [request setDelegate:self];
    
    [self.downloadHelper addDownloadRequest:request];
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 260;
}


#pragma mark - DownloadRequestDelegagte
- (void) didTaskReceiveDataFinish:(NSData *)theData request:(DownloadRequest *)request
{
    [self performSelectorOnMainThread:@selector(refreshRankingDataToCell:) withObject:request waitUntilDone:NO];
        
}

- (void) taskDataDownloadFailed:(NSError *)error request:(DownloadRequest *)request
{
    
}
@end
